<template>
  <div class="padding-container">
    <el-form ref="tableParams" :model="tableParams" size="small">
      <el-form-item>
        <el-button type="primary" size="small" @click="scanCode()">扫描二维码</el-button>
      </el-form-item>
      <el-form-item label="设备信息">
        <el-input v-model="tableParams.qr" disabled></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" size="small" @click="queryData()">查询数据</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
export default {
  data() {
    return {
      tableParams: {
        qr: null
      },
      queryParams: {
        // mid: null,
        // did: null,
        // imei: null,
        // clientid: null
        mid: '5348424D4B4A',
        did: '20220808001',
        imei: '867168840000456',
        clientid: '16631248334616'
      }
    }
  },
  methods: {
    scanCode() {
      window.location.href = "//996315.com/api/scan/"
    },
    getQrString(){
      var name = "qrresult"
      var reg = new RegExp("\\b"+ name +"=([^&]*)")
      var r = location.href.match(reg)
      if (r != null) {
        this.tableParams.qr = decodeURIComponent(r[1])
      }
    },
    queryData() {
      // var strs = this.tableParams.qr.toString().split("\n")
      // this.queryParams.imei = strs[0].substring(5)
      // this.queryParams.mid = strs[1].substring(4)
      // this.queryParams.did = strs[2].substring(4)
      // this.queryParams.clientid = new Date().getTime()
      this.send()
    },
    send() {
      this.request({
        url: '/device/qrcode/send',
        method: 'post',
        data: this.queryParams
      }).then(response => {
        alert("succ")
        let timer = setInterval(() => {
          this.query()
        }, 10000)
      }).catch(error => {
        // base.loading = false
        // this.$message({
        //   message: error.errmsg,
        //   type: 'error',
        //   duration: 3 * 1000
        // })
        alert(error.errmsg)
      })
    },
    query() {
      console.log('query')
      this.request({
        url: '/device/qrcode/query',
        method: 'post',
        data: this.queryParams
      }).then(response => {
        console.log('query succ')
        console.log(response.success)
        console.log(response.result)
      }).catch(error => {
        // base.loading = false
        // this.$message({
        //   message: error.errmsg,
        //   type: 'error',
        //   duration: 3 * 1000
        // })
        alert(error.errmsg)
      })
    },
  },
  created() {
    this.getQrString()

    // var params = {
    //   imei: '867168840000753',
    //   mid: '5348424D4B4A',
    //   did: '20220810001',
    //   clientid: String(new Date().getTime())
    // }
    // this.send(params)
  }
}
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
	.padding-container {
		text-align:left;
		// .title-part {
		// 	text-align: left;
		// 	font-size: 18px;
		// 	padding-bottom: 20px;
		// }
    background-color:yellow
	}
</style>

